Ext.onReady(function() {
	Ext.define('MenuModel', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'TITLE', type:'string'},
            {name: 'QUERYID', type:'string'},
            {name: 'ID', type:'string'},
            {name: 'PARENT', type:'string'},
            {name: 'PTITLE', type:'string'},
            {name: 'LINK', type:'string'},
            {name: 'GROUPIDS', type:'string'},
            {name: 'ICONCLS', type:'string'},
            {name: 'MENUORDER', type:'int'},
            {name: 'LEAF', type:'bool'}
        ]
    });
	
	var menuStore = Ext.create('Ext.data.Store',{
		model: 'MenuModel'
	});
	
	var menuGridRowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
        clicksToMoveEditor: 1,
        autoCancel: true,
        listeners:{
        	edit:{
        		fn:menuEdit
        	}
        }
    });
	
	var menu = Ext.create('Ext.form.field.Text',{
        fieldLabel: str_menumgr_menu,
        labelWidth: 50,
        width : 150
	});
	
	var userGroupStore2 = Ext.create('Ext.data.Store',{
		fields: [
	            { name: 'USERGROUPNAME', type: 'string'},
	            { name: 'USERGROUPID', type: 'string'}
	        ],
		 proxy:{
			type: 'ajax',
			url : getPath("/aa/po/userManager.do"),
			extraParams : {
            	"md":"readusergroup"
            },
			reader : {
				type : 'json',
				root : 'usergroup'
			}
		 }
	});
	
	var groupId2 = Ext.create('Ext.form.field.ComboBox',{
        typeAhead: true,
        triggerAction: 'all',
        selectOnTab: true,
        editable: false,
        displayField: 'USERGROUPNAME',
        valueField: 'USERGROUPID',
		store: userGroupStore2,
        lazyRender: true,
        width:120
	});
	
	var menuGrid = Ext.create('Ext.grid.Panel', {
		split: true,
		loadMask : true,
		store:menuStore,
		border: false,
		plugins: [menuGridRowEditing],
		columns: [
          {
              xtype: 'rownumberer',
              width: 30,
              hideable: false
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_common_title,
              width: 130,
              defaultWidth: 0,
              dataIndex: 'TITLE',
              hideable: false,
              editor : {
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_menumgr_menuid,
              width: 100,
              defaultWidth: 0,
              dataIndex: 'ID',
              hideable: false,
              editor : {
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_menumgr_pmenu,
              width: 100,
              defaultWidth: 0,
              dataIndex: 'PARENT',
              hideable: false,
              editor : {
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_menumgr_path,
              defaultWidth: 0,
              dataIndex: 'LINK',
              hideable: false,
              flex: 1,
              editor : {
              }
          },
          {
              xtype: 'gridcolumn',
              dataIndex: 'GROUPIDS',
              text: str_grpmgr_usergroup,
              hideable: false,
              width: 120,
              editor : {
            	  xtype: 'combobox',
                  typeAhead: true,
                  triggerAction: 'all',
                  selectOnTab: true,
                  editable: false,
                  displayField: 'USERGROUPNAME',
                  valueField: 'USERGROUPID',
                  multiSelect: true,
        		  store: userGroupStore2,
                  lazyRender: true
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_menumgr_icon,
              width: 60,
              defaultWidth: 0,
              dataIndex: 'ICONCLS',
              hideable: false,
              editor : {
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_menumgr_order,
              width: 60,
              defaultWidth: 0,
              dataIndex: 'MENUORDER',
              hideable: false,
              editor : {
              }
          },
          {
        	  xtype: 'booleancolumn',
              header: str_menumgr_isleaf,
              dataIndex: 'LEAF',
              width: 60,
              editor: {
                  xtype: 'checkbox',
                  cls: 'x-grid-checkheader-editor'
              }
          },
          {
              xtype: 'actioncolumn',
              draggable: false,
              sortable: false,
              hideable: false,
              width: 30,
              items: [
                  {
                	  icon:getPath('/common/images/delete.png'),
                	  handler : function(grid, rowIndex, colIndex){
                		  var id = menuStore.getAt(rowIndex).get('QUERYID');
                		  if (id != null && id != "") {
                			  Ext.MessageBox.confirm(str_common_confirm,str_common_confirmdel, function(button, text) {
                				  if(button == "yes") {
                					  Ext.Ajax.request({
            							  url : getPath('/aa/po/menuManager.do'),
            							  params : {
            								  md : 'deleteMenu',
            								  ID : id
            							  },
            							  actionMethods: 'POST',
            							  success : function(response) {  
            					            	var ret = Ext.JSON.decode(response.responseText);
            					            	if(ret.success){
            					            		menuGridRowEditing.cancelEdit();
            			        					var sm = menuGrid.getSelectionModel();
            			        					sm.select(rowIndex);
            			        					menuStore.remove(sm.getSelection());
            					            	}
            					            	else {
            					            		Ext.MessageBox.alert(str_common_error,ret.msg);
            					            	}
            					          }
            						  });
                				  }
                			  });
                		  }
                		  else {
                			  menuGridRowEditing.cancelEdit();
        					  var sm = menuGrid.getSelectionModel();
        					  sm.select(rowIndex);
        					  menuStore.remove(sm.getSelection());
                		  }
                	  }
                  }
              ]
          
          }
        ],
	    dockedItems: [
		  {
		      xtype: 'toolbar',
		      dock: 'top',
		      items: [
		          {
                      xtype: 'button',
                      iconCls: 'icon-add',
                      text: str_common_add,
                      listeners: {
                          click: {
                        	  fn:addMenu
                          }
                      }
                  },
		          {
                      xtype: 'tbseparator'
                  },
                  menu,
                  {
                	  xtype: 'label',
                	  text: str_grpmgr_usergroup + ":",
                	  width:50
                  },
                  groupId2,
                  {
                	  xtype: 'button',
                	  text: str_common_query,
                	  listeners: {
                          click: {
                        	  fn:queryMenu
                          }
                      }
                  }
		      ]
		   },
		]
	});
	
	function addMenu(){
		menuGridRowEditing.cancelEdit();
		var r = Ext.create('MenuModel', {
			'TITLE': '',
            'ID': '',
            'PARENT': '',
            'PTITLE': '',
            'LINK': '',
            'GROUP': '',
            'ICONCLS': '',
            'MENUORDER': '',
            'LEAF': true
        });
		menuStore.insert(0, r);
		menuGridRowEditing.startEdit(0, 0);
	}
	
	function menuEdit(editor, e){
        var TITLE = e.record.data['TITLE'];
		var QUERYID = e.record.data['QUERYID'];
		var ID = e.record.data['ID'];
		var PARENT = e.record.data['PARENT'];
		var LINK = e.record.data['LINK'];
		var GROUPIDS = e.record.data['GROUPIDS'];
		var ICONCLS = e.record.data['ICONCLS'];
		var MENUORDER = e.record.data['MENUORDER'];
		var LEAF = e.record.data['LEAF'];
		if(!isBlank(QUERYID) && QUERYID!=ID){
			Ext.MessageBox.alert(str_common_warn,str_menumgr_midchg);
			return;
		}
		if(isBlank(TITLE)||isBlank(ID)
				||isBlank(PARENT)||isBlank(MENUORDER)){
			Ext.MessageBox.alert(str_common_warn,str_menumgr_menunull);
			return;
		}
		Ext.Ajax.request({
            url : getPath("/aa/po/menuManager.do"),
            method: 'POST',
            params : {
            	"md":"updateMenu",
            	"TITLE" : TITLE,
                "QUERYID" : QUERYID,
                "ID" : ID,
                "PARENT" : PARENT,
                "LINK" : LINK,
                "GROUPIDS" : GROUPIDS,
                "ICONCLS" : ICONCLS,
                "MENUORDER" : MENUORDER,
                "LEAF" : LEAF
            },
            success : function(response) {  
            	var ret = Ext.JSON.decode(response.responseText);
            	if(ret.success){
                    e.record.data['QUERYID'] = ret.msg;
                    e.record.commit();
            	}
            	else {
            		Ext.MessageBox.alert(str_common_error,ret.msg);
            	}
            }
        });
	}
	
	function queryMenu(){
		menuStore.setProxy({
			type : 'ajax',
			actionMethods: 'POST',
			url : getPath("/aa/po/menuManager.do"),
			extraParams : {
            	"md":"readmenu",
            	"MENU":menu.getRawValue(),
            	"GROUPID":groupId2.value
            },
			reader : {
				type : 'json',
				root : 'menu',
				totalProperty : 'totalCount'
			}
		});
		menuStore.load();
	}
	
	menuMgrWin = Ext.create('Ext.Window', {
		maximizable: true,
        title: str_menumgr_menumgr,
        height: 500,
        width: 800,
        iconCls: 'icon-plugin',
        layout: 'fit',
        closeAction: 'hide',
        items: [menuGrid],
        x: 200,
        y: 100,
        listeners:{
        	hide:function(){
        		if(menuMgrBtn.pressed){
        			menuMgrBtn.toggle();
        		}
        	}
        }
    });
});